package net.sourceforge.m2settings.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableModel;

import org.apache.log4j.Logger;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

import net.sourceforge.m2settings.XMLBinder;
import net.sourceforge.m2settings.model.MirrorsTableModel;
import net.sourceforge.m2settings.model.ProxyTableModel;
import net.sourceforge.m2settings.model.Settings;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class MainWindow extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JTabbedPane jTabbedPane = null;

	private JPanel jPanel = null;

	private JTextField jTextField = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JCheckBox jCheckBox = null;

	private JLabel jLabel2 = null;

	private JCheckBox jCheckBox1 = null;

	private JLabel jLabel3 = null;

	private JCheckBox jCheckBox2 = null;

	private JPanel jPanelProxies = null;
	private JPanel jPanelMirrors = null;
	
	private Settings userSettings=null;

	private Logger log = Logger.getLogger(MainWindow.class);  //  @jve:decl-index=0:

	private JMenuBar jJMenuBar = null;

	private JMenu jMenuFile = null;

	private JMenuItem jMenuItemFileSave = null;

	private JMenuItem jMenuItemFileExit = null;
	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("General", null, getJPanel(), null);
			jTabbedPane.addTab("Proxies", null, getJPanelProxies(), "Configuration for different proxy profiles. Multiple proxy profiles might come in handy for anyone working from a notebook or other mobile platform, to enable easy switching of entire proxy configurations by simply specifying the profile id, again either from the command line or from the defaults section below.");
			jTabbedPane.addTab("Mirrors", null, getJPanelMirrors(), "Configuration of download mirrors for repositories. ");
		}
		return jTabbedPane;
	}



	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("offline");
			jLabel2 = new JLabel();
			jLabel2.setText("usePluginRegistry");
			jLabel2.setToolTipText("");
			jLabel1 = new JLabel();
			jLabel1.setText("interactiveMode");
			jLabel1.setToolTipText("");
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(4);
			gridLayout.setColumns(2);
			jLabel = new JLabel();
			jLabel.setText("localRepository");
			jLabel.setToolTipText("");
			jPanel = new JPanel();
			jPanel.setLayout(gridLayout);
			jPanel.add(jLabel, null);
			jPanel.add(getJTextField(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJCheckBox(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJCheckBox1(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(getJCheckBox2(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setText("...");
			jTextField.setToolTipText("The local repository.");
		}
		return jTextField;
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setToolTipText("Whether Maven should attempt to interact with the user for input.");
			jCheckBox.setText("not interactive");
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jCheckBox1	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox1() {
		if (jCheckBox1 == null) {
			jCheckBox1 = new JCheckBox();
			jCheckBox1.setToolTipText("Whether Maven should use the plugin-registry.xml file to manage plugin versions.");
			jCheckBox1.setText("don't use plugin-registry.xml");
		}
		return jCheckBox1;
	}

	/**
	 * This method initializes jCheckBox2	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox2() {
		if (jCheckBox2 == null) {
			jCheckBox2 = new JCheckBox();
			jCheckBox2.setText("online");
			jCheckBox2.setToolTipText("Indicate whether maven should operate in offline mode full-time.");
		}
		return jCheckBox2;
	}

	/**
	 * This method initializes jPanelProxies	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelProxies() {
		if (jPanelProxies == null) {
			TableModel proxyTableModel=new ProxyTableModel(userSettings);
			jPanelProxies = new JTablePanel("proxy",proxyTableModel);
		}
		return jPanelProxies;
	}
	/**
	 * This method initializes jPanelMirrors
	 * @return 
	 */
	private Component getJPanelMirrors() {
		if (jPanelMirrors == null) {
			TableModel mirrorTableModel=new MirrorsTableModel(userSettings);
			jPanelProxies = new JTablePanel("mirror",mirrorTableModel);
		}
		return jPanelProxies;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuFile());
		}
		return jJMenuBar;
	}



	/**
	 * This method initializes jMenuFile	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenuFile() {
		if (jMenuFile == null) {
			jMenuFile = new JMenu();
			jMenuFile.setText("File");
			jMenuFile.add(getJMenuItemFileSave());
			jMenuFile.add(getJMenuItemFileExit());
		}
		return jMenuFile;
	}



	/**
	 * This method initializes jMenuItemFileSave	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemFileSave() {
		if (jMenuItemFileSave == null) {
			jMenuItemFileSave = new JMenuItem();
			jMenuItemFileSave.setText("Save");
		}
		return jMenuItemFileSave;
	}



	/**
	 * This method initializes jMenuItemFileExit	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemFileExit() {
		if (jMenuItemFileExit == null) {
			jMenuItemFileExit = new JMenuItem();
			jMenuItemFileExit.setText("Exit");
		}
		return jMenuItemFileExit;
	}



	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				XMLBinder xmlbind=new XMLBinder();
				// the user has empty settings by default
				Settings uSettings=new Settings(); 
				try {
					FileReader fr = new FileReader(new File(System.getProperty("user.home")+"/.m2/settings.xml"));
					uSettings=xmlbind.loadSettings(fr);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MarshalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ValidationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				MainWindow thisClass = new MainWindow(uSettings);
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public MainWindow(Settings userSettings) {
		super();
		this.userSettings=userSettings;
		
		initialize();
		
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setJMenuBar(getJJMenuBar());
		this.setName("main");
		this.setContentPane(getJContentPane());
		this.setTitle("m2settings");
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

}
